﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Pigs.Weapons
{
    /// <summary>
    /// star that ninja pigs throw at player
    /// </summary>
    public class Star : pigs.DynamicObject
    {
        public Vector2 direction;

        // Animation variables
        int frameCount = 0; // Current frame
        int frameSkipX = 30; // Width of a frame
        int animationCount; // How many ticks since the last frame change.
        int animationMax; // How many ticks to change frame after. 
        Rectangle source;

        /// <summary>
        /// create the star
        /// </summary>
        /// <param name="texture">the texture to use for the star</param>
        /// <param name="position">where to draw the star</param>
        public Star(Texture2D texture, Vector2 position)
            : base(texture, position)
        {
        }

        /// <summary>
        /// initialize the star
        /// </summary>
        /// <param name="player">position of the player it's going to</param>
        /// <param name="pig">position of the pig it's from</param>
        public void Initialize(Vector2 player, Vector2 pig)
        {

            float xTogo = player.X - pig.X;
            float yTogo = player.Y - pig.Y;

            this.direction = new Vector2((float)(xTogo / Math.Sqrt(xTogo * xTogo + yTogo * yTogo)),
                (float)(yTogo / Math.Sqrt(xTogo * xTogo + yTogo * yTogo)));

            this.damage = 5;

            // Start with frame 1 of image array
            this.frameCount = 1;
            this.animationCount = 0;
            this.animationMax = 2;

            this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);

            //get the color data
            colorMap = new Color[source.Width * source.Height];
            this.texture.GetData(0, source, colorMap, 0, source.Width * source.Height);

            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);

            this.value = 25;
        }

        /// <summary>
        /// update the star's position
        /// </summary>
        public void Update()
        {
            this.position.X += this.direction.X * 7;
            this.position.Y += this.direction.Y * 7;
            // Projectiles always move to the right
            this.UpdateAnimation();

            rectangle = new Rectangle((int)position.X, (int)position.Y, source.Width, source.Height);
        }

        /// <summary>
        /// draw the star
        /// </summary>
        /// <param name="spriteBatch">the spritebatch in use by the game</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            // Update the source rectangle, based on where in the animation we are.  
            this.source.X = this.frameSkipX * this.frameCount;

            spriteBatch.Draw(texture, position, this.source, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
        }

        /// <summary>
        /// update the animation of the star
        /// </summary>
        public void UpdateAnimation()
        {
            this.animationCount += 1;
            if (this.animationCount > this.animationMax)
            {
                this.animationCount = 0;
                this.frameCount += 1;
                if (this.frameCount == 2)
                {
                    this.frameCount = 0;
                }
                this.source = new Rectangle(this.frameSkipX * this.frameCount, 0, this.frameSkipX, this.texture.Height);
                this.texture.GetData(0, source, colorMap, 0, source.Width * source.Height);
            }
        }
    }
}
